package com.qyhl.question.question;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.question.R;
import com.qyhl.question.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.question.QuestionHomeBean;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.weavey.loading.lib.LoadingLayout;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.QUESTION_HOME)
public class QuestionHomeActivity extends BaseActivity implements QuestionHomeContract.QuestionHomeView {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.duration)
    TextView duration;
    @BindView(R2.id.times)
    TextView times;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private QuestionHomeBean mBean;

    private QuestionHomePresenter mPresenter;

    @Override
    protected int getContentView() {
        return R.layout.question_activity_home;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.statusBarColor(R.color.white).statusBarDarkFont(true).fitsSystemWindows(true).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        loadMask.setStatus(LoadingLayout.Loading);
        mPresenter = new QuestionHomePresenter(this);
        mPresenter.getData();
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getData();
            }
        });
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void setData(QuestionHomeBean bean) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        mBean = bean;

        title.setText(bean.getName());
        duration.setText("2.每题答题时间" + bean.getTime() + "s,超时未答，即为答题失败");
        times.setText("3.每用户每天有" + "3" + "次答题机会；");
    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(this)) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }


    @OnClick({R2.id.back_btn, R2.id.start_btn, R2.id.rank_band})
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.back_btn) {
            finish();
        } else if (viewId == R.id.start_btn) {
            CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", mBean.getId() + "");
                        bundle.putInt("maxTime", mBean.getTime());
                        bundle.putString("title", mBean.getName());
                        RouterManager.router(ARouterPathConstant.QUESTION_CHALLENGE, bundle);
                    } else {
                        RouterManager.routerLogin(QuestionHomeActivity.this, 0);
                    }
                }

                @Override
                public void error(String s) {

                }
            });
        } else if (viewId == R.id.rank_band) {
            Bundle bundle = new Bundle();
            bundle.putString("id", mBean.getId() + "");
            RouterManager.router(ARouterPathConstant.QUESTION_RANK, bundle);
        }
    }
}
